<?php

namespace App\Lib\Tool;

/**
 * IP处理类
 *
 * @author yuefeng
 *        
 */
class Ip {
	
	/**
	 * 严格方式获取IP，不能用header伪造IP
	 */
	public static function getIp() {
		if (isset ( $_SERVER )) {
			if ($_SERVER ['REMOTE_ADDR']) {
				$realip = $_SERVER ['REMOTE_ADDR'];
			} else {
				if (getenv ( 'REMOTE_ADDR' )) {
					$realip = getenv ( 'REMOTE_ADDR' );
				}
			}
		}
		return $realip;
	}
	
	
	/**
	 * 宽松方式获取IP，客户端可伪造IP
	 */
	public static function getIpCanFark() {
		$onlineip = '';
		if (isset ( $_SERVER ['HTTP_CLIENT_IP'] ) && $_SERVER ['HTTP_CLIENT_IP'] && strcasecmp ( $_SERVER ['HTTP_CLIENT_IP'], 'unknown' )) {
			$onlineip = $_SERVER ['HTTP_CLIENT_IP'];
		} elseif (isset ( $_SERVER ['HTTP_X_FORWARDED_FOR'] ) && $_SERVER ['HTTP_X_FORWARDED_FOR'] && strcasecmp ( $_SERVER ['HTTP_X_FORWARDED_FOR'], 'unknown' )) {
			$onlineip = $_SERVER ['HTTP_X_FORWARDED_FOR'];
		} elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
			$onlineip = $_SERVER ['REMOTE_ADDR'];
		}
		return $onlineip;
	}
	
	/**
	 * 验证IP合法性
	 *
	 * @param unknown_type $ip        	
	 */
	public static function chkIp($ip) {
		$real = true;
		if (! $ip) {
			$real = false;
		}
		
		$ary = null;
		
		if ($real) {
			$ary = explode ( '.', $ip );
			if ($ary [0] < 1 || $ary [0] > 255) {
				$real = false;
			}
			
			if ($real) {
				if ($ary [1] < 0 || $ary [1] > 255) {
					$real = false;
				}
				if ($real) {
					if ($ary [2] < 0 || $ary [2] > 255) {
						$real = false;
					}
				}
				if ($real) {
					if ($ary [3] < 1 || $ary [3] > 255) {
						$real = false;
					}
				}
			}
		}
		return $real === true ? true : false;
	}
	
	/**
	 * 将IP转换为数字
	 *
	 * @param unknown_type $ip        	
	 */
	public static function ip2Long($ip) {
		$onlineip = explode ( '.', $ip );
		if (count ( $onlineip ) < 4) {
			return false;
		}
		$newIp = '';
		$tip = 0;
		foreach ( $onlineip as $v ) {
			$tip = sprintf ( '%03d', $v );
			if ($tip > 255 || $tip < 0) {
				return false;
			}
			$newIp .= $tip;
		}
		return $newIp;
	}
	
	/**
	 * 将IP C段转换为数字
	 *
	 * @param string $ip        	
	 */
	public static function ipc2Long($ip) {
		$onlineip = explode ( '.', $ip );
		if (count ( $onlineip ) < 3) {
			return false;
		}
		$newIp = '';
		$tip = 0;
		foreach ( $onlineip as $v ) {
			$tip = sprintf ( '%03d', $v );
			if ($tip > 255 || $tip < 0) {
				return false;
			}
			$newIp .= $tip;
		}
		return $newIp;
	}
	
	/**
	 * 将数字转换为IP
	 *
	 * @param unknown_type $ip        	
	 */
	public static function long2Ip($num) {
		$ipArray [0] = substr ( $num, - strlen ( $num ), strlen ( $num ) - 9 );
		$ipArray [1] = substr ( $num, - 9, 3 );
		$ipArray [2] = substr ( $num, - 6, 3 );
		$ipArray [3] = substr ( $num, - 3, 3 );
		$newIp = '';
		foreach ( $ipArray as $v ) {
			$newIp .= ( int ) $v . '.';
		}
		$newIp = trim ( $newIp, '.' );
		return $newIp;
	}
	
	/**
	 * 将数字转换为IP C段
	 * 
	 * @param int $num        	
	 */
	public static function long2Ipc($num) {
		$ipArray [0] = substr ( $num, - strlen ( $num ),3 );
		$ipArray [1] = substr ( $num, - 9, 3 );
        $ipArray [2] = substr ( $num, - 6, 3 );
		$ipArray [3] = substr ( $num, - 3, 3 );
		$newIp = '';
		foreach ( $ipArray as $v ) {
			$newIp .= ( int ) $v . '.';
		}
		$newIp = trim ( $newIp, '.' );
		return $newIp;
	}
	
	/**
	 * 获取IP段
	 * 
	 * @param int $part        	
	 */
	public static function getIpPart($ip, $part = 3) {
		if ($part < 2 || $part > 3) {
			$part = 3;
		}
		$ip = explode ( '.', $ip );
		$str = null;
		for($i = 0; $i < $part; $i ++) {
			$str .= $ip [$i] . '.';
		}
		return trim ( $str, '.' );
	}
	
	/**
	 * 将IP转换成数字,IP段亦可.该方法不可逆转
	 *
	 * @param string $ip        	
	 */
	public static function ip2Num($ip) {
		$ip = explode ( '.', $ip );
		$num += intval ( $ip [0] ) * pow ( 256, 3 );
		$num += intval ( $ip [1] ) * pow ( 256, 2 );
		$num += intval ( $ip [2] ) * 256;
		$num += intval ( $ip [3] );
		return $num;
	}

}